package com.cloud.pole.shop.controller.order;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cloud.pole.common.constant.CommonConstants;
import com.cloud.pole.common.exception.BaseException;
import com.cloud.pole.common.exception.ParamException;
import com.cloud.pole.common.msg.ApiRestResponse;
import com.cloud.pole.common.util.RegexUtils;
import com.cloud.pole.shop.model.vo.goods.GoodsCollectionVo;
import com.cloud.pole.shop.model.vo.order.ShopOrderRefundVo;
import com.cloud.pole.shop.service.order.OrderRefundService;
import com.github.pagehelper.PageInfo;

import lombok.extern.slf4j.Slf4j;

/*
*2019年3月27日
*cloud-shop
*author:frank-chen
*/
@Controller
@Slf4j
@RequestMapping("/orderRefund/front")
public class OrderRefundController {
	
	@Autowired
	private OrderRefundService orderRefundService;

	/**
	 * 申请退款或退货退款
	 * @param token
	 * @param orderItemId
	 * @param refundWay（退款方式:1：退货退款,2::仅退款）
	 * @param refundGoodStatus(收货状态（1：未收到货 2：已收到货）
	 * @param refundReason(申请退款原因)
	 * @param refundNote（说明）
	 * @param reasonImages（图片凭证）
	 * @param refundAmount（退款金额）
	 * @param userMobile（用户手机）
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/addOrderRefund")
    public ApiRestResponse<Long> addOrderRefund(
    		@RequestHeader(defaultValue = "") String token,
    		@RequestParam(defaultValue = "") String orderItemId,
    		@RequestParam(defaultValue = "") String refundWay,
    		@RequestParam(defaultValue = "1") String refundGoodStatus,
    		@RequestParam(defaultValue = "") String refundReason,
    		@RequestParam(defaultValue = "") String refundNote,
    		@RequestParam(defaultValue = "") String reasonImages,
    		@RequestParam(defaultValue = "") String refundAmount,
    		@RequestParam(defaultValue = "") String mobile){   	
    	try {
			ParamException.err(StringUtils.isBlank(token), "token不能为空");    		
			ParamException.err(StringUtils.isBlank(orderItemId), "orderItemId不能为空");
			ParamException.err(StringUtils.isBlank(refundWay), "refundWay不能为空");    		
			ParamException.err(StringUtils.isBlank(refundGoodStatus), "refundGoodStatus不能为空");
			ParamException.err(StringUtils.isBlank(refundReason), "refundReason不能为空");
			ParamException.err(StringUtils.isBlank(refundAmount), "refundAmount不能为空");
			ParamException.err(StringUtils.isBlank(mobile), "mobile不能为空");

			ParamException.err(!RegexUtils.isPositiveInteger(refundWay), "refundWay错误");
			ParamException.err(!RegexUtils.isPositiveInteger(refundGoodStatus), "refundGoodStatus错误");
			ParamException.err(!RegexUtils.isPositiveInteger(refundAmount), "refundAmount错误");

    		Long refundId = orderRefundService.addOrderRefund(token, Long.parseLong(orderItemId), Integer.parseInt(refundWay), Integer.parseInt(refundGoodStatus), 
    				refundReason, refundNote, reasonImages, Integer.parseInt(refundAmount), mobile);
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, refundId);
    	} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
	
	@ResponseBody
	@RequestMapping("/cancelOrderRefund")
    public ApiRestResponse<Boolean> cancelOrderRefund(
    		@RequestHeader(defaultValue = "") String token,
    		@RequestParam(defaultValue = "") String refundId){
		try {
			ParamException.err(StringUtils.isBlank(token), "token不能为空");    		
			ParamException.err(StringUtils.isBlank(refundId), "refundId不能为空");
			ParamException.err(!RegexUtils.isPositiveInteger(refundId), "refundId错误");
    		Boolean flag = orderRefundService.cancelOrderRefund(token, Long.parseLong(refundId));
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, flag);
    	} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
	
	@ResponseBody
	@RequestMapping("/getOrderRefundList")
    public ApiRestResponse<List<ShopOrderRefundVo>> getOrderRefundList(
    		@RequestHeader(name="token", defaultValue = "") String token,
    		@RequestParam(name="pageSize", defaultValue="") String pageSize,
    		@RequestParam(name="pageNum", defaultValue="") String pageNum
    		){
		try {
			ParamException.err(StringUtils.isBlank(token), "token不能为空");    		
			ParamException.err(StringUtils.isBlank(pageSize), "pageSize不能为空");
			ParamException.err(!RegexUtils.isPositiveInteger(pageSize), "pageSize错误");
			ParamException.err(StringUtils.isBlank(pageNum), "pageNum错误");
			ParamException.err(!RegexUtils.isPositiveInteger(pageNum), "pageNum错误");
			PageInfo<ShopOrderRefundVo> pageInfo = orderRefundService.getPageOrderRefundList(token, Integer.parseInt(pageNum), Integer.parseInt(pageSize));
			List<ShopOrderRefundVo> list = pageInfo.getList();
			return ApiRestResponse.successOfPage(CommonConstants.STATUS_OK_MESSAGE, (int)pageInfo.getPages(), pageInfo.getPageNum(), pageInfo.getPageSize(), list);
    	} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
	
	@ResponseBody
	@RequestMapping("/getOrderRefund")
    public ApiRestResponse<ShopOrderRefundVo> getOrderRefund(
    		@RequestHeader(name="token", defaultValue = "") String token,
    		@RequestParam(name="refundId", defaultValue="") String refundId){
		try {
			ParamException.err(StringUtils.isBlank(token), "token不能为空");    		
			ParamException.err(StringUtils.isBlank(refundId), "refundId不能为空");
			ParamException.err(!RegexUtils.isPositiveInteger(refundId), "refundId错误");

			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, orderRefundService.getOrderRefund(token, Long.parseLong(refundId)));
    	} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
}
